package com.sam.im.heiqun.utils;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.caiyi.fund.trade.widget.span.URLSpanWrapper;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.GroupFriendEntivity;
import com.sam.im.heiqun.entities.ImMessage;
import com.sam.im.heiqun.entities.ImageSendEntity;
import com.sam.im.heiqun.uis.activities.FriendDetailActivity;
import com.sam.im.heiqun.uis.adapters.ChatRecyclerAdapter;
import com.zq.utils.StringUtil;
import com.zq.utils.ViewUtil;


import java.util.List;

/**
 * Created by zhangqiang on 2018/7/11.
 */
public class IMMessageUtil {

    /**
     * 图片是否能转发
     *
     * @param imMessage
     * @return
     */
    public static boolean canImageForward(ImMessage imMessage) {
        if (imMessage == null) {
            return false;
        }
        ImageSendEntity sendEntity = JsonUtil.toObject(imMessage.getContent(), ImageSendEntity.class);
        return sendEntity == null || sendEntity.isCanForward();
    }


    public static String getImageUrl(String imMessageContent) {

        if (imMessageContent == null) {
            return null;
        }
        ImageSendEntity sendEntity = JsonUtil.toObject(imMessageContent, ImageSendEntity.class);
        if (sendEntity == null) {
            return null;
        }
        return sendEntity.getImagePath();
    }

    public static void swapFromIdAndDestId(ImMessage imMessage) {

        long fromId = imMessage.getFromid();
        imMessage.setFromid(imMessage.getDestid());
        imMessage.setDestid(fromId);
    }

    public static CharSequence getBeJoinGroupText(Context context,String content, Long groupId){
        JSONObject json = JSON.parseObject(content);
        String name = json.getString("name");
        List<GroupFriendEntivity> groupUserSss = ToolsUtils.getGroupUserSss(groupId);
        StringBuilder sb = new StringBuilder();
        if (groupUserSss != null && !groupUserSss.isEmpty()) {
            for (GroupFriendEntivity userSss : groupUserSss) {
                sb.append(userSss.getName());
                sb.append(",");
            }
            sb.delete(sb.length() - 1, sb.length());
        }
       return StringUtil.format(context, R.string.someone_invite_you_join_group3, name, sb.toString());
    }


    public static void setBeJoinGroupText(TextView textView, String content, Long groupId) {

        JSONObject json = JSON.parseObject(content);
        String name = json.getString("name");
        String destUserName = json.getString("destUserName");
        GroupFriendEntivity groupFriendEntivity = ToolsUtils.getGroupMemberByName(String.valueOf(groupId), name);
        if (groupFriendEntivity == null) {
            return;
        }
        List<GroupFriendEntivity> groupUserSss = ToolsUtils.getGroupUserSss(groupId);
        StringBuilder sb = new StringBuilder();
        if (groupUserSss != null && !groupUserSss.isEmpty()) {
            for (GroupFriendEntivity userSss : groupUserSss) {
                sb.append(userSss.getName());
                sb.append(",");
            }
            sb.delete(sb.length() - 1, sb.length());
        }

        final long uid = groupFriendEntivity.getUid();
        ViewUtil.setHtmlText(textView,
                StringUtil.format(textView.getContext(), R.string.someone_invite_you_join_group2, String.valueOf(uid), name, sb.toString()),
                new URLSpanWrapper.OnClickListener() {
                    @Override
                    public void onClick(View view, String s) {

                        Intent intent = new Intent(view.getContext(), FriendDetailActivity.class);
                        intent.putExtra("id", uid);
                        view.getContext().startActivity(intent);
                    }
                });
    }
}
